﻿using System;
using System.Data;
using System.Data.SqlClient;
using SW.Lite.IDAL;
using SW.Lite.Infrastructure.Common;
using SW.Lite.Infrastructure.Helpers;
using SW.Lite.Model;

namespace SW.Lite.DAL.MsSql
{
    public class Field : IField
    {
        public bool Exists(int id)
        {
            const string sql = "SELECT COUNT(1) FROM SW_Field WHERE Id=@Id";
            var paramter = new SqlParameter("@Id", SqlDbType.Int) {Value = id};
            return MsSqlHelper.Exists(sql, paramter);
        }

        public FieldModel Single(int id)
        {
            const string sql = "SELECT Id, ModelID, Code, Title, Type, Length, CodeTable FROM SW_Field WHERE Id=@Id";

            var paramter = new SqlParameter("@Id", SqlDbType.Int) {Value = id};
            return FillSingle(sql, paramter);
        }

        public PageList<FieldModel> All()
        {
            const string sql = "SELECT Id, ModelID, Code, Title, Type, Length, CodeTable FROM SW_Field";

            return FillList(sql);
        }

        public PageList<FieldModel> All(int pageIndex, int pageSize)
        {
            var start = (pageIndex - 1)*pageSize;
            var end = pageIndex*pageSize;
            var sql =
                string.Format(@"SELECT Id, ModelID, Code, Title, Type, Length, CodeTable FROM (SELECT ROW_NUMBER() OVER (ORDER BY Id) ROWNO,* FROM SW_Field) A WHERE A.ROWNO BETWEEN {0} AND {1};
            SELECT COUNT(1) FROM SW_Field;", start, end);
            return FillList(sql);
        }

        public ExecuteResult Insert(FieldModel model)
        {
            const string sql =
                "INSERT INTO SW_Field(ModelID, Code, Title, Type, Length, CodeTable) VALUES (@ModelID, @Code, @Title, @Type, @Length, @CodeTable)";

            var parameters = new[]
            {
                new SqlParameter("@ModelID", SqlDbType.Int) {Value = model.ModelID},
                new SqlParameter("@Code", SqlDbType.NVarChar) {Value = model.Code},
                new SqlParameter("@Title", SqlDbType.NVarChar) {Value = model.Title},
                new SqlParameter("@Type", SqlDbType.Int) {Value = model.Type},
                new SqlParameter("@Length", SqlDbType.Int) {Value = model.Length},
                new SqlParameter("@CodeTable", SqlDbType.NVarChar) {Value = model.CodeTable}
            };

            var result = MsSqlHelper.Execute(sql, parameters);
            return 0 == result ? new ExecuteResult(false) : new ExecuteResult(true);
        }

        public ExecuteResult Update(FieldModel model)
        {
            const string sql =
                "UPDATE SW_Field SET ModelID=@ModelID, Code=@Code, Title=@Title, Type=@Type, Length=@Length, CodeTable=@CodeTable WHERE Id=@Id ";
            var parameters = new[]
            {
                new SqlParameter("@Id", SqlDbType.Int) {Value = model.Id},
                new SqlParameter("@ModelID", SqlDbType.Int) {Value = model.ModelID},
                new SqlParameter("@Code", SqlDbType.NVarChar) {Value = model.Code},
                new SqlParameter("@Title", SqlDbType.NVarChar) {Value = model.Title},
                new SqlParameter("@Type", SqlDbType.Int) {Value = model.Type},
                new SqlParameter("@Length", SqlDbType.Int) {Value = model.Length},
                new SqlParameter("@CodeTable", SqlDbType.NVarChar) {Value = model.CodeTable}
            };

            var result = MsSqlHelper.Execute(sql, parameters);
            return 0 == result ? new ExecuteResult(false) : new ExecuteResult(true);
        }

        public ExecuteResult Delete(int id)
        {
            const string sql = "DELETE FROM SW_Field WHERE Id=@Id";
            var paramter = new SqlParameter("@Id", SqlDbType.Int) {Value = id};
            var result = MsSqlHelper.Execute(sql, paramter);
            return 0 == result ? new ExecuteResult(false) : new ExecuteResult(true);
        }

        private FieldModel FillSingle(string sql, params SqlParameter[] paramters)
        {
            FieldModel data = null;
            using (var rdr = MsSqlHelper.ExecuteReader(sql, paramters))
            {
                if (rdr.Read())
                    data = ReadData(rdr);
            }
            return data;
        }

        private PageList<FieldModel> FillList(string sql, params SqlParameter[] paramters)
        {
            var list = new PageList<FieldModel>();
            using (var rdr = MsSqlHelper.ExecuteReader(sql, paramters))
            {
                while (rdr.Read())
                    list.Add(ReadData(rdr));
                if (rdr.NextResult() && rdr.Read())
                    list.AllCount = rdr.GetInt32(0);
                else
                    list.AllCount = list.Count;
            }
            return list;
        }

        private FieldModel ReadData(SqlDataReader dr)
        {
            return new FieldModel
            {
                Id = Convert.ToInt32(dr["Id"]),
                ModelID = Convert.ToInt32(dr["ModelID"]),
                Code = Convert.ToString(dr["Code"]),
                Title = Convert.ToString(dr["Title"]),
                Type = Convert.ToInt32(dr["Type"]),
                Length = Convert.ToInt32(dr["Length"]),
                CodeTable = Convert.ToString(dr["CodeTable"])
            };
        }
    }
}